package com.key.controller.admin.assetManagement;

import com.key.common.R;
import com.key.common.dto.*;
import com.key.common.vo.AssetsTVO;
import com.key.entity.*;
import com.key.service.BorrowAssetsService;
import com.key.service.ReceiptAssetsService;
import com.key.service.ReturnAssetsService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:借用归还管理Controller
 * @author tc
 * @create 2023-02-17 11:10
 */
@Api(value = "借用与退还申请",tags = "借用与退还申请接口API")
@Controller(value = "/borrowAndReturn")
@RequestMapping(value = "/showBorrowAssets" )
public class BorrowAndReturnController {

    @Autowired
    BorrowAssetsService borrowAssetsService;

    @Autowired
    ReturnAssetsService returnAssetsService;

    /**
     * 显示借用清单
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showBorrowAssets",method = RequestMethod.POST)
    @ApiOperation(value = "借用清单表接口",notes = "借用单表的基本信息",httpMethod ="post")
    public R showBorrowAssets(@RequestBody @ApiParam(name = "borrowAssetsDTO",value = "查找时需要填入的属性",required = true) BorrowDTO borrowDTO){

        return borrowAssetsService.findBorrowAssets(borrowDTO);
    }


    /**
     * 借用清单修改
     */
    @ResponseBody
    @RequestMapping(value = "/modifyBorrowAssetsByBorrowNumber",method = RequestMethod.POST)
    @ApiOperation(value = "借用单修改接口",notes = "对借用单进行修改",httpMethod = "post")
    public R modifyBorrowAssetsByBorrowNumber(@RequestBody @ApiParam(name = "borrowDTO",value = "修改时需要填入的数据",required = true) BorrowDTO borrowDTO){
        return borrowAssetsService.editBorrowAssets(borrowDTO);
    }
    /**
     * 借用清单删除
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBorrowAssetsByBorrowNumber",method = RequestMethod.GET)
    @ApiOperation(value = "借用单删除接口",notes = "对借用单进行删除",httpMethod = "get")
    @ApiImplicitParam(name = "borrowNumbers",value = "选中行的申请单号",paramType = "query",dataType = "String")
    public R deleteBorrowAssetsByBorrowNumber(@RequestParam(name = "borrowNumbers") String borrowNumbers){
        return  borrowAssetsService.deleteBorrowAssets(borrowNumbers);
    }


    /**
     * 借用清单详情
     */
    @ResponseBody
    @RequestMapping(value = "/showAllBorrowAssets",method = RequestMethod.GET)
    @ApiOperation(value = "借用申请单详情接口",notes = "展示借用单号的详细信息",httpMethod = "get")
    @ApiImplicitParam(name = "borrowNumber",value = "选中行的单号",paramType = "query",dataType = "String")
    public R showAllBorrowAssets(@RequestParam(name = "borrowNumber") String borrowNumber){
        R r=new R();
        BorrowAssets borrowAssets=new BorrowAssets();
        borrowAssets.setBorrowNumber(borrowNumber);
        List<AssetsTVO> resultList= borrowAssetsService.findAssetsTVOByBorrowAssets(borrowAssets);
        r.setData(resultList);
        return r;
    }

    /**
     * 显示归还清单
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showReturnAssets",method = RequestMethod.POST)
    @ApiOperation(value = "归还申请表基本信息接口",notes = "归还单表的基本信息",httpMethod = "get")
    public R showReturnAssets(@RequestBody @ApiParam(name = "returnDTO",value = "查找时需要填入的属性",required = true) ReturnDTO returnDTO){
        return  returnAssetsService.findReturnAssets(returnDTO);
    }

    /**
     * 归还清单修改
     */
    @ResponseBody
    @RequestMapping(value = "/modifyReturnAssetsByReturnNumber",method = RequestMethod.POST)
    @ApiOperation(value = "归还单修改接口",notes = "对归还单进行修改",httpMethod = "post")
    public R modifyReturnAssetsByReturnNumber(@RequestBody @ApiParam(name = "returnDTO",value = "需要填入修改信息",required = true) ReturnDTO returnDTO){
        return returnAssetsService.editReturnAssets(returnDTO);
    }

    /**
     * 归还清单删除
     */
    @ResponseBody
    @RequestMapping(value = "/deleteReturnAssetsByReturnNumber",method = RequestMethod.GET)
    @ApiOperation(value = "归还单删除接口",notes = "对归还单进行删除",httpMethod = "get")
    @ApiImplicitParam(name = "returnNumbers",value = "选中行的申请单号",paramType = "query",dataType = "String")
    public R deleteReturnAssetsByReturnNumber(@RequestParam(name = "returnNumbers") String returnNumbers){
        return returnAssetsService.deleteReturnAssets(returnNumbers);
    }

    /**
     * 归还清单详情
     */
    @ResponseBody
    @RequestMapping(value = "/showAllReturnAssets",method = RequestMethod.GET)
    @ApiOperation(value = "归还申请单详情接口", notes = "展示归还单号的详细信息",httpMethod ="get")
    @ApiImplicitParam(name = "returnNumber",value = "选中的归还单号",paramType = "query",dataType = "String")
    public R showAllReturnAssets(@RequestParam(name = "returnNumber") String returnNumber){
        R r=new R();
        ReturnAssets returnAssets=new ReturnAssets();
        returnAssets.setReturnNumber(returnNumber);
        List<AssetsTVO> resultList= returnAssetsService.findAssetsTVOByReturnAssets(returnAssets);
        r.setData(resultList);
        return r;
    }


    @RequestMapping(value = "/exportBorrowExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出领用信息",notes = "把领用单信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportBorrowExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = borrowAssetsService.getWorkBook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "领用申请表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   @RequestMapping(value = "/exportReturnExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出归还信息",notes = "把领用单信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportReturnExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = returnAssetsService.getWorkBook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "归还申请表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
